$(function() {
    var layer = layui.layer
    var form = layui.form
    var laypage = layui.laypage;

    // 美化时间
    template.defaults.imports.dateFormat = function(date) {
        const dt = new Date(date)

        var y = dt.getFullYear()
        var m = dt.getMonth() + 1
        var d = dt.getDate()

        var hh = dt.getHours()
        var mm = dt.getMinutes()
        var ss = dt.getSeconds()

        return y + '-' + padZero(m) + '-' + padZero(d) + ' ' + padZero(hh) + ':' + padZero(mm) + ':' + padZero(ss)
    }

    // 补零函数
    function padZero(n) {
        return n > 9 ? n : '0' + n
    }

    // 查询的参数对象
    var q = {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
    }

    initTable()

    initCate()


    $('#form-search').on('submit', function(e) {
        e.preventDefault()
        var cate_id = $('[name=cate_id]').val()
        var state = $('[name=state]').val()
        q.cate_id = cate_id
        q.state = state
        initTable()
    })

    $('tbody').on('click', '.btn-delete', function() {
        var len = $('btn-delete').length
        var id = $(this).data('id')
        layer.confirm('确认删除?', {
                icon: 3,
                title: '提示'
            },
            function(index) {
                $.ajax({
                    method: 'get',
                    url: '/my/article/delete/' + id,
                    success: function(res) {
                        if (res.status !== 0) {
                            return layer.msg('删除文章失败！')
                        }
                        layer.msg('删除文章成功！')
                        if (len == 1) {
                            q.pagenum = q.pagenum == 1 ? 1 : q.pagenum - 1
                        }
                        initTable()
                    }
                })
                layer.close(index)
            })
    })

    function initTable() {
        $.ajax({
            method: 'get',
            url: '/my/article/list',
            data: q,
            success: function(res) {
                if (res.status !== 0) {
                    return layer.msg('获取文章列表失败！')
                }
                var temp = template('tpl-table', res)
                $('tbody').html(temp)
                renderPage(res.total)
            }
        })
    }


    function initCate() {
        $.ajax({
            method: 'get',
            url: '/my/article/cates',
            success: function(res) {
                if (res.status !== 0) {
                    return layer.msg('获取分类数据失败！')
                }
                var temp = template('tpl-cate', res)
                $('[name=cate_id]').html(temp)
                layui.form.render()
            }
        })
    }


    function renderPage(total) {
        layui.use('laypage', function() {
            laypage.render({
                elem: 'pageBox',
                count: total,
                limit: q.pagesize,
                limits: [2, 4, 6, 8, 10],
                layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
                curr: q.pagenum,
                jump: function(obj, first) {
                    q.pagenum = obj.curr
                    q.pagesize = obj.limit
                    if (!first) {
                        initTable()
                    }
                }
            });
        });
    }
})